var exec = require('child_process').execSync;
var fs = require('fs');
var HashMap = require('hashmap');

module.exports = {
    scanFilesMd5: scanFilesMd5,
    checkMd5: checkMd5
}

// +-----------------------------------------------------------------------
// |                     根据文件列表获取md5sum.dat
// +-----------------------------------------------------------------------
function scanFilesMd5( gameDir, filelist, cb ) {
    
    // 判断参数是否为数组对象
    if( !( filelist instanceof Array ) ) {
        cb( "请传入一个文件列表" );
        return;
    }
    
    // 文件写入设置
    var writeOptions = {
        encoding: "utf8",
        mode: 438,
        flag: "w"
    }
    
    // 读取文件md5写入到文件
    var fileMd5Str = "";
    for( var i = 0; i < filelist.length; i++ ) {
        var lineStr = getFileMd5( filelist[i] );
        
        if( lineStr.indexOf( gameDir ) > 0 && lineStr != "" ) {
            lineStr = lineStr.replace( gameDir, "." );
            fileMd5Str = fileMd5Str + lineStr;
        } else {
            cb( "文件路径错误" );
        }
        
        if ( ( i + 1 ) == filelist.length ) {
            fs.writeFileSync( gameDir + "/fmd5sum.dat", fileMd5Str, writeOptions );
        }
        
        cb( null, i + 1, filelist.length );
    }
    
    return;
}

// +-----------------------------------------------------------------------
// |                           获取一个文件MD5
// +-----------------------------------------------------------------------
function getFileMd5( path ) {
    try {
        return exec( "md5sum " + "'" + path + "'", { encoding: 'utf8' } );
    } catch ( err ) {
        console.error( err );
        return "";
    }
}

// +-----------------------------------------------------------------------
// |                         校验两个Md5文件的差异
// +-----------------------------------------------------------------------
function checkMd5( localFilePath, remoteFilePath ) {
    var localFileMap = new HashMap();
    var remoteFileMap = new HashMap();
    
    var localMd5Str = fs.readFileSync( localFilePath, "utf-8" );
    var remoteMd5Str = fs.readFileSync( remoteFilePath, "utf-8" );
    
    var localMd5SplitArr = localMd5Str.split( "\n" );
    for( var i = 0; i < localMd5SplitArr.length; i++ ) {
        if( localMd5SplitArr[i] != "" ) {
            var lineStr = localMd5SplitArr[i];
            var md5 = lineStr.split(" ")[0];
            var path = lineStr.split(" ")[2];
            localFileMap.set( path, md5 );
        }
    }
    
    var remoteMd5SpitArr = remoteMd5Str.split( "\n" );
    for( var i = 0; i < remoteMd5SpitArr.length; i++ ) {
        if( remoteMd5SpitArr[i] != "" ) {
            var lineStr = remoteMd5SpitArr[i];
            var md5 = lineStr.split(" ")[0];
            var path = lineStr.split(" ")[2];
            remoteFileMap.set( path, md5 );
        }
    }
    
    var localFileList = localFileMap.keys();
    
    var uploadFileList = new Array();
    for( var i = 0; i < localFileList.length; i++ ) {
        if( remoteFileMap.has( localFileList[i] ) ) {
            var localFileMd5 = localFileMap.get( localFileList[i] );
            var remoteFileMd5 = remoteFileMap.get( localFileList[i] );
            if( localFileMd5 != remoteFileMd5 ) {
                uploadFileList.push( localFileList[i] );
            }
        }
    }
    
    return uploadFileList;
}